﻿
@inject IHttpService _httpService
@inject NavigationManager NavigationManager
@inject IDataCacheService _dataCacheService
@inject IPopupService PopupService


<MDialog @bind-Value="Dialog"
         MaxWidth="440">
    <MDCard>
        <a href="@($"/entries/index/{Model.GameId}")">
            <img style="width:100%;" src="@Model.GameImage" alt="@Model.GameName">
        </a>

        <MCardTitle Class="text-h5">
            @Model.GameName
        </MCardTitle>
        <MCardText Class="pb-2">
            @Model.GameBriefIntroduction
        </MCardText>
        
        <MCardText Class="pb-2">
            @if (Model.IsScored)
            {
                <CnGalWebSite.Shared.MasaComponent.Shared.Components.MasaRating Disable Value="Model.MusicSocre" Label="音乐" />
                <CnGalWebSite.Shared.MasaComponent.Shared.Components.MasaRating Disable Value="Model.PaintSocre" Label="美术" />
                <CnGalWebSite.Shared.MasaComponent.Shared.Components.MasaRating Disable Value="Model.ScriptSocre" Label="剧本" />
                <CnGalWebSite.Shared.MasaComponent.Shared.Components.MasaRating Disable Value="Model.ShowSocre" Label="演出" />
                <CnGalWebSite.Shared.MasaComponent.Shared.Components.MasaRating Disable Value="Model.SystemSocre" Label="程序" />
                @if (Model.CVSocre > 0 && Model.IsDubbing)
                {
                    <CnGalWebSite.Shared.MasaComponent.Shared.Components.MasaRating Disable Value="Model.CVSocre" Label="配音" />

                }
                <CnGalWebSite.Shared.MasaComponent.Shared.Components.MasaRating Disable Value="Model.TotalSocre" Label="总评" />


            }
            else
            {
                <div class="pb-2">
                    未评分
                </div>
            }
        </MCardText>

        <MCardText Class="pb-2">
            <GameRecordTagCard Model="Model" IsDark="true" />
        </MCardText>

        @if (IsCurrentUser)
        {
            <MCardTitle Class="text-h6 pt-0">
                评语
            </MCardTitle>
            <MCardText Class="pb-2" Style=" white-space: break-spaces;"> 
                @(string.IsNullOrWhiteSpace(Model.PlayImpressions)?"无": Model.PlayImpressions)
            </MCardText>
        }

        @if (IsCurrentUser)
        {
            <MCardActions Class="ps-2 pe-2">

                <MButton Color="green darken-1"
                         Text
                         OnClick="OnHidden">
                    @(Model.IsHidden? "显示" : "隐藏")
                </MButton>


                <MSpacer></MSpacer>
                <MButton Color="green darken-1"
                         Text
                         OnClick="OnClickEdit">
                    编辑
                </MButton>
            </MCardActions>
        }

    </MDCard>
</MDialog>


@code {
    public GameRecordViewModel Model { get; set; }
    [Parameter]
    public EventCallback<GameRecordViewModel> OnEdit { get; set; }
    [Parameter]
    public EventCallback OnChanged { get; set; }

    [Parameter]
    public bool IsCurrentUser { get; set; }

    [CascadingParameter]
    public ErrorHandler ErrorHandler { get; set; }
    bool Dialog { get; set; }

    public async Task OnClickEdit()
    {
        Dialog = false;
        await OnEdit.InvokeAsync(Model);
    }

    public async Task OnHidden()
    {
        try
        {
            var obj = await _httpService.PostAsync<HiddenGameRecordModel, Result>("api/playedgame/HiddenGameRecord", new HiddenGameRecordModel { IsHidden = !Model.IsHidden, GameIds = new int?[] { Model.GameId } });
            //判断结果
            if (obj.Successful == false)
            {
                await PopupService.ToastErrorAsync("修改游玩记录显示状态失败", obj.Error);
            }
            else
            {
                Model.IsHidden = !Model.IsHidden;
                await PopupService.ToastSuccessAsync("修改游玩记录显示状态成功", "修改游玩记录显示状态成功");
                Dialog = false;
               await OnChanged.InvokeAsync();
            }
        }
        catch (Exception ex)
        {
            await ErrorHandler.ProcessError(ex, "修改游玩记录显示状态失败");
        }
    }


    public void Refresh(bool dialog, GameRecordViewModel model)
    {
        Dialog = dialog;
        Model = model;
        StateHasChanged();
        
    }
}
